home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP02.ZIP / CHAP02 / PATRON / PATRON.CPP < prev    next >
C/C++ Source or Header  |  1993-05-25  |  9KB  |  383 lines

  1. /*
  2.  * PATRON.CPP
  3.  * Original Starter Chapter 2
  4.  *
  5.  * WinMain which is all we need for the basic application.
  6.  *
  7.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Software Design Engineer
  10.  * Microsoft Systems Developer Relations
  11.  *
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17.  
  18. #include "patron.h"
  19.  
  20.  
  21.  
  22.  
  23. /*
  24.  * WinMain
  25.  *
  26.  * Purpose:
  27.  *  Main entry point of application.   Should register the app class
  28.  *  if a previous instance has not done so and do any other one-time
  29.  *  initializations.
  30.  */
  31.  
  32. int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev
  33.     , LPSTR pszCmdLine, int nCmdShow)
  34.     {
  35.     LPCPatronFrame  pFR;
  36.     FRAMEINIT       fi;
  37.     WPARAM          wRet;
  38.  
  39.     //Attempt to allocate and initialize the application
  40.     pFR=new CPatronFrame(hInst, hPrev, pszCmdLine, nCmdShow);
  41.  
  42.     fi.idsMin=IDS_FRAMEMIN;
  43.     fi.idsMax=IDS_FRAMEMAX;
  44.     fi.idsStatMin=IDS_STATMESSAGEMIN;
  45.     fi.idsStatMax=IDS_STATMESSAGEMAX;
  46.     fi.idStatMenuMin=ID_MENUFILE;
  47.     fi.idStatMenuMax=ID_MENUHELP;
  48.     fi.iPosWindowMenu=WINDOW_MENU;
  49.     fi.cMenus=CMENUS;
  50.  
  51.     //If we can initialize pFR, start chugging messages
  52.     if (pFR->FInit(&fi))
  53.         wRet=pFR->MessageLoop();
  54.  
  55.     delete pFR;
  56.     return wRet;
  57.     }
  58.  
  59.  
  60.  
  61.  
  62. /*
  63.  * CPatronFrame::CPatronFrame
  64.  * CPatronFrame::~CPatronFrame
  65.  *
  66.  * Constructor Parameters:
  67.  *  hInst           HINSTANCE from WinMain
  68.  *  hInstPrev       HINSTANCE from WinMain
  69.  *  pszCmdLine      LPSTR from WinMain
  70.  *  nCmdShow        int from WInMain
  71.  */
  72.  
  73. CPatronFrame::CPatronFrame(HINSTANCE hInst, HINSTANCE hInstPrev
  74.     , LPSTR pszCmdLine, int nCmdShow)
  75.     : CFrame(hInst, hInstPrev, pszCmdLine, nCmdShow)
  76.     {
  77.     return;
  78.     }
  79.  
  80.  
  81. CPatronFrame::~CPatronFrame(void)
  82.     {
  83.     return;
  84.     }
  85.  
  86.  
  87.  
  88.  
  89. /*
  90.  * CPatronFrame::CreateCClient
  91.  *
  92.  * Purpose:
  93.  *  Constructs a new client specific to the application.
  94.  *
  95.  * Parameters:
  96.  *  None
  97.  *
  98.  * Return Value:
  99.  *  LPCClient       Pointer to the new client object.
  100.  */
  101.  
  102. LPCClient CPatronFrame::CreateCClient(void)
  103.     {
  104.     return (LPCClient)(new CPatronClient(m_hInst));
  105.     }
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. /*
  113.  * CPatronFrame::FRegisterAllClasses
  114.  *
  115.  * Purpose:
  116.  *  Registers all classes used in this application.
  117.  *
  118.  * Parameters:
  119.  *  None
  120.  *
  121.  * Return Value:
  122.  *  BOOL            TRUE if registration succeeded, FALSE otherwise.
  123.  */
  124.  
  125. BOOL CPatronFrame::FRegisterAllClasses(void)
  126.     {
  127.     WNDCLASS        wc;
  128.  
  129.     //First let the standard frame do its thing
  130.     if (!CFrame::FRegisterAllClasses())
  131.         return FALSE;
  132.  
  133.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  134.     wc.hInstance     = m_hInst;
  135.     wc.cbClsExtra    = 0;
  136.     wc.lpfnWndProc   = PagesWndProc;
  137.     wc.cbWndExtra    = CBPAGESWNDEXTRA;
  138.     wc.hIcon         = NULL;
  139.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  140.     wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1);
  141.     wc.lpszMenuName  = NULL;
  142.     wc.lpszClassName = SZCLASSPAGES;
  143.  
  144.     if (!RegisterClass(&wc))
  145.         return FALSE;
  146.  
  147.     return TRUE;
  148.     }
  149.  
  150.  
  151.  
  152.  
  153.  
  154. /*
  155.  * CPatronFrame::OnCommand
  156.  *
  157.  * Purpose:
  158.  *  WM_COMMAND handler for the Patron frame window that processes extra
  159.  *  File menu items as well as the Page menu.
  160.  *
  161.  * Parameters:
  162.  *  hWnd            HWND of the frame window.
  163.  *  wParam          WPARAM of the message.
  164.  *  lParam          LPARAM of the message.
  165.  *
  166.  * Return Value:
  167.  *  LRESULT         Return value for the message.
  168.  */
  169.  
  170. LRESULT CPatronFrame::OnCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
  171.     {
  172.     LPCPatronDoc    pDoc;
  173.  
  174.     COMMANDPARAMS(wID, wCode, hWndMsg);
  175.  
  176.     /*
  177.      * Don't bother with anything during first initialization,
  178.      * skipping many GizmoBar notifications.
  179.      */
  180.     if (m_fInit)
  181.         return 0L;
  182.  
  183.     pDoc=(LPCPatronDoc)m_pCL->ActiveDocument();
  184.  
  185.     switch (wID)
  186.         {
  187.         case IDM_FILEPRINT:
  188.             pDoc->Print(m_hWnd);
  189.             return 0L;
  190.  
  191.         case IDM_FILEPRINTERSETUP:
  192.             pDoc->PrinterSetup(m_hWnd, FALSE);
  193.             return 0L;
  194.  
  195.  
  196.         case IDM_PAGENEWPAGE:
  197.             pDoc->NewPage();
  198.             break;
  199.  
  200.         case IDM_PAGEDELETEPAGE:
  201.             pDoc->DeletePage();
  202.             break;
  203.  
  204.         case IDM_PAGENEXTPAGE:
  205.             pDoc->NextPage();
  206.             break;
  207.  
  208.         case IDM_PAGEPREVIOUSPAGE:
  209.             pDoc->PreviousPage();
  210.             break;
  211.  
  212.  
  213.         case IDM_PAGEFIRSTPAGE:
  214.             pDoc->FirstPage();
  215.             break;
  216.  
  217.         case IDM_PAGELASTPAGE:
  218.             pDoc->LastPage();
  219.             break;
  220.  
  221.  
  222.         default:
  223.            return CFrame::OnCommand(hWnd, wParam, lParam);
  224.         }
  225.  
  226.     return 0L;
  227.     }
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236. /*
  237.  * CPatronFrame::CreateGizmos
  238.  *
  239.  * Purpose:
  240.  *  Procedure to create all the necessary gizmobar buttons.
  241.  *
  242.  * Parameters:
  243.  *  None
  244.  *
  245.  * Return Value:
  246.  *  UINT            Number of gizmos added to the bar.
  247.  */
  248.  
  249. UINT CPatronFrame::CreateGizmos(void)
  250.     {
  251.     UINT            iLast;
  252.     UINT            uState=GIZMO_NORMAL;
  253.     UINT            utCmd =GIZMOTYPE_BUTTONCOMMAND;
  254.  
  255.     //Insert the standard ones.
  256.     iLast=CFrame::CreateGizmos();
  257.  
  258.     //Insert Print File Import in the 5th position and account for it in iLast.
  259.     m_pGB->Add(utCmd, 4, IDM_FILEPRINT, m_dxB, m_dyB, NULL, NULL, 6, uState);
  260.     iLast++;
  261.  
  262.     //Add New Page, and Delete Page
  263.     m_pGB->Add(utCmd, iLast++, IDM_PAGENEWPAGE,    m_dxB, m_dyB, NULL, m_hBmp, 2, uState);
  264.     m_pGB->Add(utCmd, iLast++, IDM_PAGEDELETEPAGE, m_dxB, m_dyB, NULL, m_hBmp, 3, uState);
  265.  
  266.     //Separator
  267.     m_pGB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB, NULL, NULL, 0, uState);
  268.  
  269.     //First, Prev, Next, Last pages.
  270.     m_pGB->Add(utCmd, iLast++, IDM_PAGEFIRSTPAGE,    m_dxB, m_dyB, NULL, m_hBmp, 4, uState);
  271.     m_pGB->Add(utCmd, iLast++, IDM_PAGEPREVIOUSPAGE, m_dxB, m_dyB, NULL, m_hBmp, 5, uState);
  272.     m_pGB->Add(utCmd, iLast++, IDM_PAGENEXTPAGE,     m_dxB, m_dyB, NULL, m_hBmp, 6, uState);
  273.     m_pGB->Add(utCmd, iLast++, IDM_PAGELASTPAGE,     m_dxB, m_dyB, NULL, m_hBmp, 7, uState);
  274.  
  275.     return iLast;
  276.     }
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284. /*
  285.  * CPatronFrame::UpdateMenus
  286.  *
  287.  * Purpose:
  288.  *  Handles the WM_INITMENU message for the frame window.  Depending
  289.  *  on the existence of an active window, menu items are selectively
  290.  *  enabled and disabled.
  291.  *
  292.  * Parameters:
  293.  *  hMenu           HMENU of the menu to intialize
  294.  *  iMenu           UINT position of the menu.
  295.  *
  296.  * Return Value:
  297.  *  None
  298.  */
  299.  
  300. void CPatronFrame::UpdateMenus(HMENU hMenu, UINT iMenu)
  301.     {
  302.     LPCDocument pDoc;
  303.     BOOL        fOK=FALSE;
  304.     BOOL        fCallDefault=TRUE;
  305.     UINT        uTemp;
  306.     UINT        uTempE;
  307.     UINT        uTempD;
  308.  
  309.     pDoc=m_pCL->ActiveDocument();
  310.  
  311.     uTempE=MF_ENABLED | MF_BYCOMMAND;
  312.     uTempD=MF_DISABLED | MF_GRAYED | MF_BYCOMMAND;
  313.     uTemp=((NULL!=pDoc) ? uTempE : uTempD);
  314.  
  315.     //File menu:  If there is no current document window, disable Import.
  316.     if (m_phMenu[0]==hMenu)
  317.         {
  318.         EnableMenuItem(hMenu, IDM_FILEPRINT, uTemp);
  319.         EnableMenuItem(hMenu, IDM_FILEPRINTERSETUP, uTemp);
  320.         }
  321.  
  322.     //Page menu
  323.     if (m_phMenu[2]==hMenu)
  324.         {
  325.         EnableMenuItem(hMenu, IDM_PAGENEWPAGE,      uTemp);
  326.         EnableMenuItem(hMenu, IDM_PAGEDELETEPAGE,   uTemp);
  327.         EnableMenuItem(hMenu, IDM_PAGENEXTPAGE,     uTemp);
  328.         EnableMenuItem(hMenu, IDM_PAGEPREVIOUSPAGE, uTemp);
  329.         EnableMenuItem(hMenu, IDM_PAGEFIRSTPAGE,    uTemp);
  330.         EnableMenuItem(hMenu, IDM_PAGELASTPAGE,     uTemp);
  331.         }
  332.  
  333.  
  334.     if (fCallDefault)
  335.         CFrame::UpdateMenus(hMenu, iMenu);
  336.  
  337.     return;
  338.     }
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345. /*
  346.  * CPatronFrame::UpdateGizmos
  347.  *
  348.  * Purpose:
  349.  *  Enables and disables gizmos depending on whether we have
  350.  *  a document or not.
  351.  *
  352.  * Parameters:
  353.  *  None
  354.  *
  355.  * Return Value:
  356.  *  None
  357.  */
  358.  
  359. void CPatronFrame::UpdateGizmos(void)
  360.     {
  361.     LPCDocument pDoc;
  362.     BOOL        fEnable;
  363.  
  364.     //Let the default hack on its gizmos.
  365.     CFrame::UpdateGizmos();
  366.  
  367.     pDoc=m_pCL